package com.wensi.module_developer

import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.wensi.adapter.CommonAdapter
import com.wensi.adapter.CommonViewHolder
import com.wensi.helper.ARouterHelper
import com.wensi.lib_base.BaseActivity
import com.wensi.lib_voice.manager.VoiceManager
import com.wensi.lib_voice.tts.VoiceTTS
import com.wensi.module_developer.data.DeveloperListData
import com.wensi.utils.L
import kotlinx.android.synthetic.main.activity_devevloper.*

@Route(path = ARouterHelper.PATH_DEVELOPER)
class DeveloperActivity : BaseActivity() {

    private val TYPE_TITLE = 0 // 标题
    private val TYPE_CONTENT = 1 // 内容

    private val mList = ArrayList<DeveloperListData>()

    override fun getLayoutId(): Int {
        return R.layout.activity_devevloper
    }

    override fun getTitleText(): String {
        return getString(R.string.developer_name)
    }

    override fun isShowBack(): Boolean {
        return true
    }

    override fun initView() {
        initData()
        initListView()
    }
    // 初始化数据
    private fun initData() {
        val dataArray = resources.getStringArray(R.array.DeveloperListArray)
        dataArray.forEach {
            if (it.contains("[")) {
                addItemData(TYPE_TITLE, it.replace("[", "").replace("]", ""))
            } else {
                addItemData(TYPE_CONTENT, it)
            }
        }
    }
//    初始化列表
    private fun initListView() {
        rvDeveloperView.layoutManager = LinearLayoutManager(this) // 布局管理器
        // 添加 分割线
        rvDeveloperView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        // 适配器
        rvDeveloperView.adapter = CommonAdapter(mList, object: CommonAdapter.OnMoreBindDataListener<DeveloperListData>{
            override fun getItemViewType(position: Int): Int {
                return mList[position].type
            }

            override fun onBindViewHolder(
                model: DeveloperListData, viewHolder: CommonViewHolder, type: Int, position: Int) {
                when (model.type) {
                    TYPE_TITLE -> {
                        viewHolder.setText(R.id.mTvDeveloperTitle, model.text)
                    }
                    TYPE_CONTENT -> {
                        viewHolder.setText(R.id.mTvDeveloperContent, "${position}#${model.text}")
                        viewHolder.itemView.setOnClickListener {
                            itemOnClickListener(position)
                        }
                    }
                }
            }

            override fun getLayoutId(type: Int): Int {
               return if (type == TYPE_TITLE) {
                    R.layout.layout_developere_title
                } else {
                    R.layout.layout_developere_content // mTvDeveloperContent
                }
            }
        })
    }

    // 添加数据
    private fun addItemData(type: Int, text: String) {
        mList.add(DeveloperListData(type, text))
    }

    // 内容的点击事件
    private fun itemOnClickListener (position: Int) {
        when(position) {
            1 -> ARouterHelper.startActivity(ARouterHelper.PATH_APP_MANAGER)
            2 -> ARouterHelper.startActivity(ARouterHelper.PATH_CONSTELLATION)
            3 -> ARouterHelper.startActivity(ARouterHelper.PATH_JOKE)
            4 -> ARouterHelper.startActivity(ARouterHelper.PATH_MAP)
            5 -> ARouterHelper.startActivity(ARouterHelper.PATH_SETTING)
            6 -> ARouterHelper.startActivity(ARouterHelper.PATH_VOICE_SETTING)
            7 -> ARouterHelper.startActivity(ARouterHelper.PATH_WEATHER)

            // 开始识别短语音
            9 -> VoiceManager.startAsr()
            10 -> VoiceManager.stopAsr()
            11 -> VoiceManager.cancelAsr()
            12 -> VoiceManager.releaseAsr()

            // 开始唤醒
            14 -> VoiceManager.startWakeup()
            15 -> VoiceManager.stopWakeup()

            // 开始讲话
            20 -> VoiceManager.startSpeak("喝水", object: VoiceTTS.OnTTSResultListener{
                override fun ttsEnd() {
                    // 语音播放完毕，回调
                    L.i("语音播放完毕，执行回调方法 ttsEnd()")
                }
            })
            21 -> VoiceManager.pauseSpeak()
            22 -> VoiceManager.resumeSpeak()
            23 -> VoiceManager.stopSpeak()
            24 -> VoiceManager.releaseSpeak()
        }
    }
}
